# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from common.utils.config import ConfigUtil
from apps.admin.schemas.setting.ai import dialog_schema as schema


class AiDialogService:

    @classmethod
    async def detail(cls) -> schema.AiDialogVo:
        config = await ConfigUtil.get("dialog") or {}
        search = await ConfigUtil.get("web_search") or {}
        search_zhipu = search.get("zhipu") or {}

        return schema.AiDialogVo(
            default_robot=config.get("default_robot",  ""),
            currency_unit=config.get("currency_unit",  ""),
            win_watermark=config.get("win_watermark",  ""),
            web_search=schema.WebSearch(
                status=int(search.get("status") or 0),
                engine=search.get("engine", "zhipu"),
                zhipu=schema.WebSearch.EngineParams(
                    search_engine=search_zhipu.get("search_engine", "search_std"),
                    api_key=search_zhipu.get("api_key", ""),
                    points=search_zhipu.get("points", 0)
                )
            )
        )

    @classmethod
    async def save(cls, post: schema.AiDialogIn):
        await ConfigUtil.set("dialog", "default_robot", post.default_robot)
        await ConfigUtil.set("dialog", "currency_unit", post.currency_unit)
        await ConfigUtil.set("dialog", "win_watermark", post.win_watermark)

        await ConfigUtil.set("web_search", "status", post.web_search.status)
        await ConfigUtil.set("web_search", "engine", post.web_search.engine)
        await ConfigUtil.set("web_search", "zhipu", post.web_search.zhipu.model_dump())
